package com.qdxwx.data.service.member;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.mapper.member.MemberTypeMapper;
import com.qdxwx.data.service.course.CourseMemberService;
import com.qdxwx.models.member.MemberType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * MemberType接口
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class MemberTypeServiceImpl implements MemberTypeService {

    @Autowired
    private MemberTypeMapper memberTypeMapper;
    @Autowired
    private CourseMemberService courseMemberService;

    /**
     * 修改MemberType
     */
    public int update(MemberType memberType) {
        return memberTypeMapper.update(memberType);
    }

    /**
     * 根据id获取单个MemberType对象
     */
    public MemberType get(int id) {
        if (id < 1) return null;
        return memberTypeMapper.selectById(id);
    }

    /**
     * 添加会员类型
     */
    public int insert(MemberType memberType) {
        return memberTypeMapper.insert(memberType);
    }

    @Override
    public List<MemberType> getList() {
        return memberTypeMapper.selectList(new MemberType(), null);
    }

    @Override
    public List<MemberType> getList(MemberType memberType, PageRequest page) {
        return memberTypeMapper.selectList(memberType, page);
    }

    @Override
    public List<MemberType> getListByCourseId(int courseId) {
        if (courseId < 1) return null;
        return memberTypeMapper.selectListByCourseId(courseId);
    }

    @Override
    public int count(MemberType memberType) {
        return memberTypeMapper.count(memberType);
    }

    @Override
    public int deleteByIds(String ids) {
        String[] arrIds = TKConvert.toStrArray(ids);
        if (arrIds.length < 1) return 0;
        int row = memberTypeMapper.deleteByIds(arrIds);
        if (row > 0) {//同时删除关联数据
            for (String id : arrIds) {
                courseMemberService.delete(TKConvert.toLong(id), 0);
            }
        }
        return row;
    }

}